package app.cn.com.school.activity

import android.content.Context
import android.os.Bundle
import android.text.InputType
import android.text.TextUtils
import android.widget.Toast
import app.cn.com.school.R
import app.cn.com.school.api.Api
import app.cn.com.school.api.AppRequest
import app.cn.com.school.api.bean.response.TestResponse
import app.cn.com.school.databinding.ActivitySignBinding
import app.cn.com.school.tools.Constants
import app.cn.com.school.tools.CountDownTimerUtils
import app.cn.com.watering.api.ApiUtill
import kotlinx.android.synthetic.main.activity_add_list_address.*

class SignActivity : BaseActivity<ActivitySignBinding>() {
    override fun initBindingVar() {
    }

    override fun initTitle() {
        binding.titlebar.back.setOnClickListener { finish() }
        binding.titlebar.title.text = "注册"
    }

    lateinit var mCountDownTimerUtils: CountDownTimerUtils
    lateinit var context: Context
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        initBinding(R.layout.activity_sign)
        context = this
        initView()
    }

    private fun initView() {
        binding.sendCode.setOnClickListener {
            sendCodeHttp()
        }
        binding.seePwd.setOnClickListener {
            var type = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
            if (binding.signPwd.inputType == type) {
                binding.seePwd.setImageResource(R.mipmap.see)
                binding.signPwd.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
                binding.signPwd.setSelection(binding.signPwd.text.length)
            } else {
                binding.seePwd.setImageResource(R.mipmap.no_see)
                binding.signPwd.inputType = type
                binding.signPwd.setSelection(binding.signPwd.text.length)
            }
        }

        binding.signSubmit.setOnClickListener {
            isNull()
        }

        binding.signAgreement.setOnClickListener {
            //用户协议

        }
    }

    private fun isNull() {
        var isNull = true
        var msg = ""
        if (!Constants.validMobile(binding.signPhone.text.toString().trim())) {
            isNull = false
            msg = "请输入正确手机号"
        }

        if (TextUtils.isEmpty(binding.signCode.text.toString().trim())) {
            isNull = false
            msg = "请输入验证码"
        }

        if (TextUtils.isEmpty(binding.signPwd.text.toString().trim())) {
            isNull = false
            msg = "请输入密码"
        }
        if (isNull) {
            signHttp()
        } else {
            showTips(msg)
            return
        }
    }


    /**
     * 发送验证码
     * */
    private fun sendCodeHttp() {
        if (!Constants.validMobile(binding.signPhone.text.toString().trim())) {
            showTips("请输入正确手机号")
            return
        }
        ApiUtill.getInstance().getApi(AppRequest.getAPI().sendCode(binding.signPhone.text.toString().trim(), "1"), { baseResponse ->
            baseResponse as TestResponse
            if (baseResponse.code == 200) {
                showTips("验证码已发生到您的手机,请注意查收")
                mCountDownTimerUtils = CountDownTimerUtils(binding.sendCode, 60000, 1000)
                mCountDownTimerUtils.start()
            } else {
                showTips("验证码发送失败，请重新发送")
            }
        })
    }

    /**
     * 注册
     * */
    private fun signHttp() {
        ApiUtill.getInstance().getApi(AppRequest.getAPI().register(binding.signPhone.text.toString().trim(), binding.signCode.text.toString().trim(), binding.signPwd.text.toString().trim()), { baseResponse ->
            baseResponse as TestResponse
            if (baseResponse.code == 200) {
                showTips("注册成功")
                finish()
            } else {
                showTips("注册失败，请重新提交")
            }
        })


    }


}
